import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// 获取命令行参数中的模式
const mode = process.env.MODE || 'development';

// 加载 .env 文件中的环境变量
const env = loadEnv(mode, process.cwd());

// https://vite.dev/config/
export default defineConfig({
  base: '/static',
  server: {
    host: "0.0.0.0",
    port: 18888,
    proxy: {
      '/op': {
        target: env.VITE_TARGET_URL,
        changeOrigin: true,
        rewrite: (path) => path
      },
    }
  },
  plugins: [
    vue(),
    vueDevTools(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
})
